import openpyxl

def GetMuslDynamicSyms(nmLogFile):
    symsSet = set()
    with open(nmLogFile) as f:
        nmLogs = f.readlines()
        # print(len(nmLogs))
        for log in nmLogs:
            desLog = log.split()
            if len(desLog) != 3:
                print("Error found in {0} {1}".format(log, desLog))
                return None
            # print(desLog[2])
            if desLog[1].isupper():
                symsSet.add(desLog[2])
    # print(symsSet)
    # print(len(symsSet))
    return symsSet

def GetMuslStaticSyms(nmLogStaticFile):
    symsSet = set()
    with open(nmLogStaticFile) as f:
        nmLogs = f.readlines()
        # print(len(nmLogs))
        for log in nmLogs:
            log = log.strip()
            if log.endswith('.o:') or log == '':
                # print(log)
                continue
            desLog = log.split()
            if desLog[-1].startswith('$'):
                continue
            symsSet.add(desLog[-1])
    # print(symsSet)
    # print(len(symsSet))
    return symsSet

def MergeMuslSyms(*symsSet):
    allSyms = set()
    for syms in symsSet:
        print(len(syms))
        allSyms.update(syms)
    print(len(allSyms))
    return allSyms

def ReadBlackBionicSyms(xlsxPath):
    try:
        wb = openpyxl.load_workbook(xlsxPath)
    except:
        print(f'load ndk file {xlsxPath} failed')
        return None
    dataList = []
    sheet_names = wb.sheetnames
    for sheet_name in sheet_names:
        sht = wb[sheet_name]
        tmpList = []
        for row in sht.rows:
            rowData = []
            if row[0].value == None:
                continue
            for cell in row:
                rowData.append(cell.value)
            tmpList.append(rowData)
        del tmpList[0] # del title
    dataList += tmpList
        # print(dataList[0])
        # print(dataList[1])
        # print(dataList[-1])
    return dataList

def FindMuslHaveSymsInBlackBionicSyms(muslSyms, blackBionicSyms):
    foundSyms = []
    for i in range(len(blackBionicSyms)):
        if blackBionicSyms[i][0] in muslSyms:
            print('Found One: {0}'.format(blackBionicSyms[i][0]))
            foundSyms.append(blackBionicSyms[i][0])
            blackBionicSyms[i][4] = 'MUSL_SUPPORTED'
    print(len(foundSyms))
    return foundSyms

def WriteBlackBionicSyms(xlsxPath, blackBionicSyms):
    try:
        wb = openpyxl.load_workbook(xlsxPath)
    except:
        print(f'load ndk file {xlsxPath} failed')
        return None
    sheet = wb.create_sheet('bionic_black_list', 0)
    #表头
    sheet.cell(row = 1, column = 1, value = 'symbol')
    sheet.cell(row = 1, column = 2, value = 'type')
    sheet.cell(row = 1, column = 3, value = 'header')
    sheet.cell(row = 1, column = 4, value = 'modules/class')
    sheet.cell(row = 1, column = 5, value = 'status')
    sheet.cell(row = 1, column = 6, value = 'symbol_on_openharmony')
    sheet.cell(row = 1, column = 7, value = 'remarks')
    i = 2
    for line in blackBionicSyms:
        sheet.cell(row = i, column = 1, value = line[0])
        sheet.cell(row = i, column = 2, value = line[1])
        sheet.cell(row = i, column = 3, value = line[2])
        sheet.cell(row = i, column = 4, value = line[3])
        sheet.cell(row = i, column = 5, value = line[4])
        sheet.cell(row = i, column = 6, value = line[5])
        sheet.cell(row = i, column = 7, value = line[6])
        i += 1
    wb.save(xlsxPath)

if __name__ == '__main__':
    nmLogFile = 'musl_dynamic_syms.txt'
    dynamicSymsSet = GetMuslDynamicSyms(nmLogFile)
    nmLogStaticFile = 'musl_static_syms.txt'
    staticSymsSet = GetMuslStaticSyms(nmLogStaticFile)
    muslSyms = MergeMuslSyms(dynamicSymsSet, staticSymsSet)
    blackBionicSyms = ReadBlackBionicSyms('../black_list/bionic.xlsx')
    # print(len(blackBionicSyms))
    # print(len(blackBionicSyms[-1]))
    FindMuslHaveSymsInBlackBionicSyms(muslSyms, blackBionicSyms)
    WriteBlackBionicSyms('../black_list/bionic.xlsx', blackBionicSyms)